package com.dqyt.app.service;

import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.mapper.HistoryMapper;
import com.dqyt.khjc.mapper.SealMapper;
import com.dqyt.khjc.mapper.UserMapper;
import com.dqyt.khjc.mongo.WorkDao;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class AppUserService {

    @Autowired
    public UserMapper userMapper;

    @Autowired
    private HistoryMapper historyMapper;


    @Autowired
    private SealMapper sealMapper;

    @Autowired
    private WorkDao mWorkDao;

    /**
     * 查询某个角色的所有用户
     * @param request
     * @param role
     * @return
     */
    public ResultMsg getUserByRole(HttpServletRequest request,long userId, String token,  long role){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();


            List<User>  data = new ArrayList<>();
            List<User>  alldata = userMapper.getUserList2();
            for(User user:alldata){
                String jsStr = user.getRole();
                String [] jsArray =jsStr.split(",");
                List<String> listB= Arrays.asList(jsArray);
                if(listB.contains(String.valueOf(role))){
                    data.add(user);
                }
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(data);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 查询所有用户
     * @param request
     * @return
     */
    public ResultMsg getUserALL(HttpServletRequest request,long userId, String token){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();
            List<User> data = userMapper.getUserList();
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(data);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 修改密码
     *
     * @param request
     * @param oldPassword
     * @param newPassword
     * @param newPassword2
     * @return
     */
    public ResultMsg updatePwd(HttpServletRequest request,long userId, String token,  String oldPassword, String newPassword, String newPassword2) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            if (null == oldPassword || "".equals(oldPassword)
                    || null == newPassword || "".equals(newPassword)
                    || null == newPassword2 || "".equals(newPassword2)
                    || !newPassword.equals(newPassword2)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }
            User nUser = userMapper.getUserById(userId);
            if (!nUser.getPassword().equals(oldPassword)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("旧密码错误");
                return ret;
            }
            nUser.setPassword(newPassword);
            int res = userMapper.updateUserById(nUser);
            if (res > 0) {
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("修改成功");
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + nUser.getUserName() + "]修改了密码");
                historyBean.setType(Opt.GRXXGL_GRXXSZ_XXMM);
                historyBean.setOpUserId(nUser.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(nUser.getUserId()));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(nUser.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(nUser.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            } else {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("修改失败，数据异常");
                return ret;
            }


        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 获取我的印章列表
     *
     * @param request
     * @return
     */
    public ResultMsg getMyseaAll(HttpServletRequest request,long userId, String token) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            //分页查询数据
            List<SealBean> datas = sealMapper.getMysealAll(userId);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(datas);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 设置印章的启用禁用状态
     *
     * @param request
     * @param sealId  0代表禁用
     * @return
     */
    public ResultMsg setSealType(HttpServletRequest request,long userId, String token,long sealId) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User nUser = userMapper.getUserById(userId);
            nUser.setSealId(sealId);
            int res = userMapper.updateUserById(nUser);
            if (res > 0) {
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("修改成功");
                //添加操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                if (sealId == 0) {
                    historyBean.setMessage("[" + nUser.getUserName() + "]停用了了印章");
                    historyBean.setType(Opt.GRXXGL_GRXXSZ_JYYZ);
                } else {
                    historyBean.setMessage("[" + nUser.getUserName() + "]启用了了印章");
                    historyBean.setType(Opt.GRXXGL_GRXXSZ_QYYZ);
                }
                historyBean.setOpUserId(nUser.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(nUser.getUserId()));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(nUser.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(nUser.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            } else {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("修改失败，数据异常");
                return ret;
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     *
     * @param request
     * @param role
     */
    public ResultMsg  getUserByRoleForWork(HttpServletRequest request,long userId, String token, long role) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            List<User>  data = new ArrayList<>();
            List<User>  alldata = userMapper.getUserList2();
            for(User user:alldata){
                String jsStr = user.getRole();
                String [] jsArray =jsStr.split(",");
                List<String> listB= Arrays.asList(jsArray);
                if(listB.contains(String.valueOf(role))){
                    data.add(user);
                }
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(data);

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 查询某个角色的用户 包含印章信息   lifubo(new)
     *
     * @param request
     * @return
     */
    public ResultMsg getUserListJc(HttpServletRequest request, long userId, String token,String role) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 首先取得用户信息
            //User user = UserInfoUtil.getUser_Web(request);
            List<User>  data = new ArrayList<>();
            List<User>  alldata = userMapper.getUserListJc();
            for(User user:alldata){
                //base64图片加水印
                String jsStr = user.getRole();
                String [] jsArray =jsStr.split(",");
                List<String> listB= Arrays.asList(jsArray);
                if(listB.contains(role)){
                    data.add(user);
                }
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(data);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询用户
     *
     * @param request
     * @return
     */
    public ResultMsg getLoginByUserId(HttpServletRequest request, long userId, String token) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            User user = UserInfoUtil.getUserInfologin(userId, token);
            if(null == user){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("未登录");
                return ret;
            }else{
                ret.setCode(ResultMsg.SUCESS);
                ret.setData(user);
                ret.setMsg("成功");
                return ret;
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询自己的信息
     * @param request
     * @param userId
     * @param token
     */
    public ResultMsg getUserByIpApp(HttpServletRequest request, long userId, String token){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = new ResultMsg(); // 声明一个返回值

            User user =  userMapper.getUserById(userId);
            if(null !=user){
                user.setPassword("");
                ret.setCode(ResultMsg.SUCESS);
                ret.setData(user);
                ret.setMsg("成功");
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }





    /**
     * 查询某个角色的用户 包含印章信息   lifubo(new)
     *
     * @param request
     * @return
     */
    public ResultMsg getAppUserListJcLhq(HttpServletRequest request, long userId, String token,String role) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 首先取得用户信息
            //User user = UserInfoUtil.getUser_Web(request);
            List<User>  data = new ArrayList<>();
            List<User>  alldata = userMapper.getUserListJc();
            for(User user:alldata){
                if(user.getMtype()==1 || user.getMtype()==8 ||user.getMtype()==10){
                    data.add(user);
                }
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(data);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

}
